package com.gperp.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gperp.common.PortalResult;
import com.gperp.controller.LogAspect;
import com.gperp.mapper.AsKeywordsMapper;
import com.gperp.mapper.other.KeyWordsMapper;
import com.gperp.mapper.other.SystemConfigMapper2;
import com.gperp.pojo.AsAccountdetail;
import com.gperp.pojo.AsKeywords;
import com.gperp.pojo.AsKeywordsExample;
import com.gperp.pojo.AsUser;
import com.gperp.pojo.Result;
import com.gperp.service.AccountDetailService;
import com.gperp.service.AskeysWordsService;

@Service("askeysWordsService")
public class AskeysWordsServiceImpl implements AskeysWordsService{
	@Autowired
	private AsKeywordsMapper asKeywordsMapper;
	@Autowired
	private KeyWordsMapper keyWordsMapper;
	@Autowired
	private  SystemConfigMapper2 systemConfigMapper;
	@Resource
	private AccountDetailService accountDetailService;
	@Autowired
    private LogAspect aspect;
	@Override
	public Result querylist(int page,int rows,String keywords) {
		AsKeywordsExample exaple = new AsKeywordsExample();
		if (keywords!=null &&!keywords.equals("")) {
			exaple.createCriteria().andKeywordsLike("%"+keywords+"%");
		}
		// 设置分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		List<AsKeywords> list = asKeywordsMapper.selectByExample(exaple);
		// 取分页结果
		PageInfo<AsKeywords> pageInfo = new PageInfo<>(list);
		Result r = new Result();
		r.setRows(pageInfo.getList());
		r.setPageIndex(page);
		r.setTotal(pageInfo.getTotal());		
		return r;
	}
	@Override
	public AsKeywords querybyid(Long id) {
		// TODO Auto-generated method stub
		return asKeywordsMapper.selectByPrimaryKey(id);
	}
	@Override
	public int update(AsKeywords keyword) {
		int t=0;
		if(keyword.getPrice()>=0){
			t=asKeywordsMapper.updateByPrimaryKeySelective(keyword);
		}
		return t;
	}
	@Override
	public int insert(AsKeywords keyword) {
		// TODO Auto-generated method stub
		//解决插入关键词日期不正确
		keyword.setRegdatetime(new Date());
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(new Date());
		rightNow.add(Calendar.YEAR,keyword.getServiceyears());
		keyword.setRegpassdatetime(rightNow.getTime());
		keyword.setCheckstatus(1);
		keyword.setIsuse(1);
		keyword.setLoginurl(systemConfigMapper.selectApp(null).getConfigvalue());
		//扣费
		AsAccountdetail accountDetail = new AsAccountdetail();
		AsUser user= aspect.getUser();
		accountDetail.setMemo("用户"+user.getUsername()+"申请关键词‘"+keyword.getKeywords()+"’消费");
		accountDetail.setMoney(keyword.getPrice()-keyword.getPrice()*2);//取反
		accountDetail.setDetailtypename("消费");
		accountDetail.setUserid(user.getId());
		accountDetailService.saveAccount(accountDetail);
		return asKeywordsMapper.insertSelective(keyword);
	}
	@Override
	public AsKeywords querykeyword(String keywords) {
		// TODO Auto-generated method stub
		return  keyWordsMapper.queryByKeyWords(keywords);
	}

}
